<!-- 回收预约时间配置 -->
<template>
    <div >
        <div class='table-page-search-wrapper'>
            <a-form layout='inline'>
               <a-row :gutter='48'>
                    <!-- <a-col :md='8' :sm='24'>
                        <a-form-item label="标题名称">
                            <a-input v-model="name" allow-clear placeholder="请输入分类名称" />
                        </a-form-item>
                    </a-col> -->
                    <a-col :md='8' :sm='24'>
                        <a-form-item label="状态">
                            <a-select v-model="status" placeholder="请选择" allowClear @change="getData">
                                <a-select-option v-for="(item,i) in statusList" :key="i" :value="item.id">{{item.name}}</a-select-option>
                            </a-select>
                        </a-form-item>
                    </a-col>
                    <a-col :md='8' :sm='24'>
                    </a-col>
                    <a-col :md='8' :sm='24'>
                        <a-form-item >
                            <a-button type='primary' style="margin-right: 20px" @click="getData">查询</a-button>
                            <a-button type="" icon="plus" @click="add()">添加</a-button>
                        </a-form-item>
                    </a-col>
                </a-row>
            </a-form>
        </div>  

        <a-table rowKey="id" :loading="isload" :columns="columns" :data-source="dataList" @change="handleTableChange" :pagination="{ pageSize: 10, total: total ,showSizeChanger: true,pageSize: size, }" size="small">
            <div slot="status" slot-scope="status">
                <a-switch checked-children="开" un-checked-children="关" disabled :checked="status == 1 ? true : false" />
            </div>
            <div slot="operation" slot-scope="text,record">
                <a-button type="link" @click="update(record)">修改</a-button>
                <a-popconfirm
                    title="是否确认删除当前配置?"
                    ok-text="Yes"
                    cancel-text="No"
                    @confirm="del(record)"
                >
                    <a-button type="link">删除</a-button>
                </a-popconfirm>
            </div>
        </a-table>

        <a-modal
            title="添加、修改预约时间"
            :visible="visible"
            @ok="handleOk"
            :confirm-loading="confirmLoading"
            @cancel="visible = false"
            >
            <div>
                <a-form :label-col="{ span: 8 }" :wrapper-col="{ span: 14 }">
                    <a-form-item label="预约时间">
                        <div class="flex alignCenter">
                            <a-time-picker v-model="item.startTime" format="HH:mm" valueFormat="HH:mm"/>
                            <div>-</div>
                            <a-time-picker v-model="item.endTime" format="HH:mm" valueFormat="HH:mm"/>
                        </div>
                    </a-form-item>
                    <a-form-item label="排序权重" help="值越大，越靠前">
                        <a-input v-model="item.sort" placeholder="请输入排序权重" allow-clear></a-input>
                    </a-form-item>
                    <a-form-item label="状态">
                        <a-radio-group v-model="item.status" >
                            <a-radio  v-for="(item,i) in statusList" :key="i" :value="item.id">{{item.name}}</a-radio>
                        </a-radio-group>
                    </a-form-item>
                </a-form>
            </div>
        </a-modal>
    </div>
</template>

<script>
import { queryPetReservationTime , updatePetReservationTime } from '@/api/pet';
import { columns } from './table';
import moment from 'moment';
export default {
    data () {
        return {
            brandId: this.global.brandId,
            name: '', // 名称
            status: undefined, // 状态
            statusList: [
                { id: '0', name: '关闭'},{ id: '1', name: '开启'}
            ],
            columns,
            dataList: [],
            total: 0,
            isload: false,
            page: 1,
            size: 10,
            item: {
                id: '',
                name: '',
                sort: '',
                startTime: '',
                endTime: '',
                status: '',
            },
            confirmLoading: false,
            isAdd: true,
            visible: false,
        };
    },
    async created(){
        try {
            this.getData()
        } catch (error) {
            this.$notification['error']({
                message: '提示',
                description: error,
                duration: 8,
            })
        }
    },
    methods: {
        moment,
        // 查询数据
        getData(){
            let _this = this
            _this.isload = true
            const params = {
                'brandId': _this.global.brandId,
                'status': _this.status,
                'sort': 'sort',
                'size':_this.size,
                'page':_this.page - 1,
            }
            queryPetReservationTime(params)
            .then(res=>{
                _this.isload = false
                if(res.success){
                    _this.dataList = res.data.content
                    _this.total = res.data.totalElements
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
        // 分页
        handleTableChange(pagination, filters, sorter) {
            console.log(pagination);
            this.page = pagination.current;
            this.size = pagination.pageSize;
            this.getData()
        },
        // 添加
        add(){
            this.isAdd = true;
            this.item.id = '';
            this.item.startTime = '';
            this.item.endTime = '';
            this.item.sort = '1';
            this.item.status = '1';
            this.visible = true
        },
        // 修改
        update(item){
            let _this = this;
            // _this.item = JSON.parse(JSON.stringify(item))
            _this.item.id = item.id;
            _this.item.sort = item.sort;
            _this.item.reservationTime = item.reservationTime;
            _this.item.status = item.status;
            // _this.item.startTime = _this.moment(item.pickupTime.split('-')[0],'HH:mm');
            // _this.item.endTime = _this.moment(item.pickupTime.split('-')[1],'HH:mm');
            _this.item.startTime = (_this.item.reservationTime.split('-')[0]);
            _this.item.endTime = (_this.item.reservationTime.split('-')[1]);
            _this.isAdd = false;
            _this.visible = true
        },
        // 确认添加、修改
        handleOk(){
            let _this = this
            console.log(_this.item.startTime,'startTAime')
            console.log(_this.item.endTime,'startTAime')
            // console.log(_this.item.startTime.format('HH:mm'),'startMomentTime');
            if(_this.item.startTime == '' || _this.item.endTime == ''){
                _this.$message.warning('请选择预约时间')
                return;
            }
            const params = {
                'id': _this.item.id,
                // 'pickupTime': `${(_this.item.startTime).format('HH:mm')}-${(_this.item.endTime).format('HH:mm')}`,
                'reservationTime': `${_this.item.startTime}-${_this.item.endTime}`,
                'sort': _this.item.sort,
                'status': _this.item.status,
                'brandId': _this.brandId,
            }
            updatePetReservationTime(params)
            .then(res=>{
                if(res.success){
                    _this.visible = false;
                    _this.getData();
                    _this.$notification['success']({
                        message: '提示',
                        description: _this.isAdd ? '添加成功' : '修改成功',
                        duration: 8,
                    })
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
        // 删除
        del(item){
            let _this = this
            const params = {
                'id': item.id,
                'isDelete': '1'
            }
            updatePetReservationTime(params)
            .then(res=>{
                if(res.success){
                    _this.getData();
                    _this.$notification['success']({
                        message: '提示',
                        description: '删除成功',
                        duration: 8,
                    })
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
        startChange(time) {
            console.log(time);
            this.item.startTime = time;
        },
        endChange(time) {
            console.log(time);
            this.item.endTime = time;
        },
    }
}

</script>
<style scoped>

</style>